declare var xgpush: any;
export class PushService {


  /**
   * 注册推送
   * @param account  (可选绑定账户)
   */
  static registerXGPush(account:string) {
    return new Promise(function (resolve, reject) {
      xgpush.registerPush(account, function (event) {
        let str =  "注册推送服务成功,设备token为：" + event.data;
        if(!PushService.isNullOrEmpty(account))
           str = "注册并绑定账号成功,设备token为：" + event.data;
        resolve(str);
      }, function (event) {
        let str = "注册推送服务失败";
        if(!PushService.isNullOrEmpty(account)){
          　str = "注册并绑定账号失败";
        }
        reject(str)
      });
    })
  }




    /**
   * 绑定账号
   * @param account　必填业务账号
   */
  static bindAccount(account:string){
    return new Promise(function (resolve,reject) {
      xgpush.bindAccount(account,function(event){
        let str = "绑定账号" + account +"成功,设备token为：" + event.data;
        resolve(str);
      },function(event){
        let str = "绑定账号失败";
        reject(str)
      });
    })

  }

  /**
   * 解绑账号
   * @param account (可选)
   */
  static unBindAccount(account:string){
    return new Promise(function (resolve,reject) {
      xgpush.unRegisterPush(account,function(event){
        let str = "推送反注册成功";
        resolve(str);
      },function(event){
        let str = "推送反注册失败";
        reject(str)
      });
    })
  }

  /**
   * 通知回调 Android only
   */
  static onShow(){
    return new Promise(function (resolve) {
      xgpush.on("show",function (event) {
        resolve(event)
      })
    })
  }

  /**
   * 应用内消息　Android only
   */
  static onMessage(){
    return new Promise(function (resolve) {
      xgpush.on("message",function (event) {
        resolve(event)
      })
    })
  }

  /**
   * 点击通知触发　Android & iOS
   */
  static onClick(){
    return new Promise(function (resolve) {
      xgpush.on("click",function (event) {
        resolve(event)
      })
    })
  }


  public static isNullOrEmpty(value:string){
    return value == null || value.trim().length == 0 ?true:false;
  }


}
